package com.ouyeel.wlxer.activity;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.widget.TextView;

import com.ouyeel.wlxer.R;
import com.ouyeel.wlxer.base.BaseAppStar;
import com.ouyeel.wlxer.bean.VersionIn;
import com.ouyeel.wlxer.bean.VersionOut;
import com.ouyeel.wlxer.down.DemoService;
import com.ouyeel.wlxer.down.core.AllenChecker;
import com.ouyeel.wlxer.down.core.VersionParams;
import com.ouyeel.wlxer.model.LoginModel;
import com.ouyeel.wlxer.model.imp.LoginModelImp;
import com.ouyeel.wlxer.util.Config;
import com.ouyeel.wlxer.util.SharePrefUtil;
import com.ouyeel.wlxer.util.StringUtils;

import butterknife.BindView;

/**
 * Created by rankaifeng on 2017/10/19.
 */

public class AppStart extends BaseAppStar {
    @BindView(R.id.tv_version)
    TextView tvVersion;
    private boolean isLogin;
    private LoginModel loginModel;
    String versionCode;
    PackageInfo mPackageInfo;
    public static AppStart appStart;

    @Override
    public int getLayout() {
        return R.layout.appstart;
    }


    @Override
    public void initView() {
        appStart = this;
        isLogin = SharePrefUtil.getBoolean(this, "isLogin", false);
        loginModel = new LoginModelImp();
        showVersion();
    }

    private void requestVersion() {
        // TODO: 2017/10/23 调网络请求获取最新版本号跟当前对比，如果当前版本号小于服务器返回的版本号就
        // TODO: 2017/10/23  提示更新
        VersionIn mVersionIn = new VersionIn();
        mVersionIn.setPackage_name("com.baosaas.carapp");
        mVersionIn.setApp_type(Config.APP_TYPE);
//        mVersionIn.setDeviceId(DeviceUtils.getUniqueId(this));
        loginModel.getAppVersion(this, mVersionIn);
    }

    /**
     * 显示版本号
     */
    private void showVersion() {
        try {
            mPackageInfo = getPackageManager()
                    .getPackageInfo(getApplication().getPackageName(), 0);
            versionCode = mPackageInfo.versionName;//获取版本号
            if (StringUtils.isNotEmpty(versionCode)) {
                tvVersion.setText("V" + versionCode);
                SharePrefUtil.saveString(this, "version", versionCode);
            }
            requestVersion();
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void requestVersionSuccess(VersionOut out) {
        SharePrefUtil.saveBoolean(this, "isForce", false);
        if (versionCode.compareTo(out.getData().getCurrent_version()) < 0) {
            // TODO: 2017/11/9 需要版本更新
            VersionParams.Builder builder = new VersionParams.Builder()
                    .setRequestUrl(Config.BASE_URL)
                    .setService(DemoService.class);
            builder.setShowDownloadingDialog(true);
            builder.setShowNotification(true);
            builder.setOnlyDownload(true)
                    .setDownloadUrl(out.getData().getUrl())
                    .setTitle("检测到新版本：" + out.getData().getCurrent_version())
                    .setUpdateMsg(out.getData().getUpgrade_detail());
            AllenChecker.startVersionCheck(this, builder.build());
        } else {
            changeActivity();
        }
    }

    public void changeActivity() {
        if (isLogin) {
            MainActivity.gotoActivity(AppStart.this);
            return;
        }
        LoginActivity.gotoActivity(AppStart.this);
    }
}
